package mysale.web;

import mysale.pojo.Food;
import mysale.service.FoodService;
import mysale.service.impl.FoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author SnakeWoodMan
 * @create 2020-12-24 16:20
 */
public class FoodServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static String selectedFid = null;
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operation = request.getParameter("op");
        FoodService foodService = new FoodServiceImpl();
        String key = null;
        key = request.getParameter("key");//从search搜索框获取key
//        System.out.println("kong:"+key);
        if ("searchFood".equals(operation)&& !key.isEmpty()){//若操作为搜索框且搜索框内容非空，则对搜索框内容进行搜索
//            key = URLEncoder.encode(key,"utf-8");
            List<Food> foodList = foodService.selectlikename(key);
//            System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
//            System.out.println(key);
//            System.out.println(foodList.size());
            request.getSession().setAttribute("foodList",foodList);
//            request.setCharacterEncoding("utf-8");
            request.getRequestDispatcher("/foodList.jsp").forward(request, response);

//            if(foodList!=null){
//                request.getSession().setAttribute("foodList",foodList);
////                System.out.println(food.getFoodname());
//                request.getRequestDispatcher("/foodList.jsp").forward(request, response);
//            }else {
//                request.setAttribute("msg","错误");
//            }
        }else if ("searchFood".equals(operation) && key.isEmpty()){//若操作为搜索框且搜索框内容空，则对查找所有数据
//            List<Food> foodList = new FoodServiceImpl().searchAllFood();
            List<Food> foodList = foodService.searchAllFood();
//            System.out.println("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
//            System.out.println("key:" + key.isEmpty());
//            System.out.println(foodList.size());
            request.getSession().setAttribute("foodList",foodList);
            request.getRequestDispatcher("/foodList.jsp").forward(request, response);
        }else if (operation.equals("scan")){
            String fid = request.getParameter("fid");
            Food food = null;
            List<Food> list = (List<Food>) request.getSession().getAttribute("foodList");
//            System.out.println("长度:"+list.size());
//            System.out.println("fid="+fid);
//            for (int i=0;i<list.size();i++){
//                System.out.println("m.getFid="+list.get(i).getFid());
//                System.out.println(list.get(i).getFid());
//            }
            for (Food m: list ){
                int intfid = m.getFid();
                String strfid = Integer.toString(intfid);
                if (fid.equals(strfid)){
                    food = m;
                    break;
                }
            }

            selectedFid = request.getQueryString();
            System.out.println("selected fid: "+selectedFid);
            request.getSession().setAttribute("food",food);
            request.getRequestDispatcher("/showfood.jsp").forward(request, response);

        }else if(operation.equals("getbyftid")){
            String ftid = request.getParameter("ftid");
            System.out.println("ftid:"+ftid);
            List<Food> foodList = foodService.selectbyfoodtype(ftid);
            request.getSession().setAttribute("foodList",foodList);
            request.getRequestDispatcher("/foodList.jsp").forward(request, response);
        }
    }
}
